package com.ruoyi.boat.service.impl;

import com.ruoyi.boat.domain.Alert;
import com.ruoyi.boat.domain.Boat;
import com.ruoyi.boat.domain.Statistic;
import com.ruoyi.boat.dto.BoatDto;
import com.ruoyi.boat.mapper.BoatMapper;
import com.ruoyi.boat.service.*;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 波浪艇Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@Service
public class BoatServiceImpl implements IBoatService {
    @Autowired
    private BoatMapper boatMapper;

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IStatusService statusService;

    @Autowired
    private IAlertService alertService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IStatisticService statisticService;

    /**
     * 查询波浪艇
     *
     * @param id 波浪艇主键
     * @return 波浪艇
     */
    @Override
    public Boat selectBoatById(Long id) {
        return boatMapper.selectBoatById(id);
    }

    /**
     * 查询波浪艇列表
     *
     * @param boat 波浪艇
     * @return 波浪艇
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "b")
    public List<Boat> selectBoatList(Boat boat) {
        return boatMapper.selectBoatList(boat);
    }

    /**
     * 新增波浪艇
     *
     * @param boat 波浪艇
     * @return 结果
     */
    @Override
    public int insertBoat(Boat boat) {
        boat.setCreateTime(DateUtils.getNowDate());
        return boatMapper.insertBoat(boat);
    }

    /**
     * 修改波浪艇
     *
     * @param boat 波浪艇
     * @return 结果
     */
    @Override
    public int updateBoat(Boat boat) {
        boat.setUpdateTime(DateUtils.getNowDate());
        return boatMapper.updateBoat(boat);
    }

    /**
     * 批量删除波浪艇
     *
     * @param ids 需要删除的波浪艇主键
     * @return 结果
     */
    @Override
    public int deleteBoatByIds(String ids) {
        return boatMapper.deleteBoatByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除波浪艇信息
     *
     * @param id 波浪艇主键
     * @return 结果
     */
    @Override
    public int deleteBoatById(Long id) {
        return boatMapper.deleteBoatById(id);
    }

    @Override
    public BoatDto selectBoatDto(Long id) {
        Boat boat = this.selectBoatById(id);
        BoatDto boatDto = new BoatDto();
        BeanUtils.copyProperties(boat, boatDto);
        boatDto.setDeviceList(deviceService.selectDeviceListByBoatId(id));
        boatDto.setStatusList(statusService.selectStatusListByBoatCode(boat.getCode()));
        boatDto.setAlertList(alertService.selectAlertList(new Alert().setBoatId(id)));
        Statistic statistic = statisticService.selectStatisticListOfToday(new Statistic().setBoatCode(boat.getCode()));
        boatDto.setStatistic(statistic);
        return boatDto;
    }

    @Override
    public boolean deleteImgByBoatId(Long id) {
        Boat boat = this.selectBoatById(id);
        if (boat.getImg() != null && !boat.getImg().isEmpty()) {
            String path = RuoYiConfig.getProfile() + StringUtils.substringAfter(boat.getImg(), Constants.RESOURCE_PREFIX);
            File file = new File(path);
            if (file.exists()) {
                return file.delete();

            }
        }
        return false;
    }
}
